/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.drypisy;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import javax.swing.JPanel;

/**
 *
 * @author Marcin Piłat
 */
public class WarpDialog extends javax.swing.JDialog {

    private WarpProcess proc = new WarpProcess();
    private BufferedImage image;
    private BufferedImage orgImage;
    private ImagePanel imgPanel;
    private AppControler ctrl;
    private CirclePanel cPanel;
    private double prop;
    
    public WarpDialog(java.awt.Frame parent, boolean modal, BufferedImage img, AppControler c) {
        super(parent, modal);
        initComponents();
        ctrl = c;
        imgPanel = ImagePanel.getSmallImagePanel(img);
        image = imgPanel.getImage();
        prop = (double)img.getWidth()/(double)image.getWidth();
        ColorModel cm = image.getColorModel();
        boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
        WritableRaster raster = image.copyData(null);
        orgImage =  new BufferedImage(cm, raster, isAlphaPremultiplied, null);
        //imgPanel.setSize(image.getWidth(),image.getHeight());
        //lPanel.setSize(image.getWidth(),image.getHeight());
        lPanel.add(imgPanel,new Integer(0));
        cPanel = new CirclePanel();
        cPanel.setSize(image.getWidth(),image.getHeight());
        lPanel.add(cPanel,new Integer(1));
        setSize(image.getWidth()+ 50, image.getHeight() + 100);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lPanel = new javax.swing.JLayeredPane();
        jButton1 = new javax.swing.JButton();
        jSlider1 = new javax.swing.JSlider();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Przekształć");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jSlider1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jSlider1MouseReleased(evt);
            }
        });
        jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSlider1StateChanged(evt);
            }
        });

        jButton2.setText("Wróć");
        jButton2.setEnabled(false);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jSlider1, javax.swing.GroupLayout.DEFAULT_SIZE, 224, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addComponent(lPanel))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton1)
                        .addComponent(jButton2))
                    .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        proc.countProp(prop);
        ctrl.setProcess(proc);
        ctrl.process();
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSlider1StateChanged
        int r = jSlider1.getValue();
        proc.setR(r);
        makeLookahead();
    }//GEN-LAST:event_jSlider1StateChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        proc.backMemento();
        image = proc.process(orgImage);
        imgPanel.setImage(image);
        setWindowSize(image.getWidth(), image.getHeight());
        if(proc.mem.getSize() == 0){
            jButton2.setEnabled(false);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jSlider1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jSlider1MouseReleased
        int r = jSlider1.getValue();
        proc.setR(r);
        makeFinalLookahead();
    }//GEN-LAST:event_jSlider1MouseReleased

    private void makeLookahead(){
        BufferedImage tmpImage = proc.oneProcess(image);
        imgPanel.setImage(tmpImage);
        setWindowSize(tmpImage.getWidth(), tmpImage.getHeight());
    }
    
    private void makeFinalLookahead(){
        proc.addMemento(cPanel.start[0], cPanel.start[1], cPanel.end[0], cPanel.end[1], jSlider1.getValue());
        image = proc.oneProcess(image);
        imgPanel.setImage(image);
        setWindowSize(image.getWidth(), image.getHeight());
        jButton2.setEnabled(true);
    }
    
    private void setWindowSize(int w, int h){
        int ow = getWidth();
        int oh = getHeight();
        int nw, nh;
        w+=30;
        h+=50;
        if(ow < w ){
            nw = w;
        }else{
            nw = ow;
        }
        if(oh < h ){
            nh = h;
        }else{
            nh = oh;
        }
        setSize(nw, nh);
        imgPanel.setSize(w, h);
        cPanel.setSize(w, h);
        lPanel.setSize(w, h);
    }
    
    private class CirclePanel extends JPanel{
        
        int start [] = {-1,-1};
        int end [] = {0,0};
        boolean pressed = false;
        
        public CirclePanel() {
            setBackground(new Color(0,0,0,0));
            setOpaque(false);
            this.addMouseMotionListener(new MouseMotionListener() {
                @Override
                public void mouseDragged(MouseEvent e) {
                    
                }
                @Override
                public void mouseMoved(MouseEvent e) {
                    if(pressed){
                        end[0] = e.getX();
                        end[1] = e.getY();
                        proc.setCoords(start[0], start[1], end[0], end[1]);
                        makeLookahead();
                        repaint();
                    }else{ 
                    }
                }
            });
            this.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if(!pressed){
                        pressed = true;
                        start[0] = e.getX();
                        start[1] = e.getY();
                        end[0] = e.getX();
                        end[1] = e.getY();
                        proc.setCoords(start[0], start[1], end[0], end[1]);
                        makeLookahead();
                        repaint();
                    }else{
                        pressed = false;
                        if(start[0] != -1){
                            end[0] = e.getX();
                            end[1] = e.getY();
                            proc.setCoords(start[0], start[1], end[0], end[1]);
                            makeFinalLookahead();
                            repaint();
                        }
                    }
                }

                @Override
                public void mousePressed(MouseEvent e) {
                    
                }
                @Override
                public void mouseReleased(MouseEvent e) {
                    
                }
                @Override
                public void mouseEntered(MouseEvent e) {}
                @Override
                public void mouseExited(MouseEvent e) {  }
            });
        }
        
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(new Color(99, 131, 255));
            g2d.drawLine(start[0], start[1], end[0], end[1]);
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JSlider jSlider1;
    private javax.swing.JLayeredPane lPanel;
    // End of variables declaration//GEN-END:variables
}
